Figure 46. Sharing Database Connections
Because Enterprise Objects Framework uses the minimum number of connections by default, you don't need to do anything to limit the number of connections an application uses. However, you can close connections when they aren't in use.
public void closeChannels() {In Objective-C:
int i, contextCount, j, channelCount;
NSArray contexts;
EOObjectStoreCoordinator coordinator;
coordinator = (EOObjectStoreCoordinator)EOObjectStoreCoordinator.defau ltCoordinator();
contexts = coordinator.cooperatingObjectStores();
contextCount = contexts.count();
for (i = 0; i < contextCount; i++) {
NSArray channels = ((EODatabaseContext)contexts.objectAtIndex(i)).registere dChannels();
channelCount = channels.count();
for (j = 0; j < channelCount; j++) {
((EODatabaseChannel)channels.objectAtIndex(j)).adaptorCh annel().closeChannel();
}
}
}
- (void)closeChannelsThe closeChannels method gets the EODatabaseContexts from the default EOObjectStoreCoordinator. Then it gets the EODatabaseChannels registered with each EODatabaseContext. To close the database connection managed by an EODatabaseChannel, closeChannels sends the channel's EOAdaptorChannel a closeChannel message.
{
int i, contextCount, j, channelCount;
NSArray *contexts;
EOObjectStoreCoordinator *coordinator;
coordinator = [EOObjectStoreCoordinator defaultCoordinator];
contexts = [coordinator cooperatingObjectStores];
contextCount = [contexts count];
for (i = 0; i < contextCount; i++) {
NSArray *channels = [(EODatabaseContext *)
[contexts objectAtIndex:i] registeredChannels];
channelCount = [channels count];
for (j = 0; j < channelCount; j++) {
[[(EODatabaseChannel *)
[channels objectAtIndex:j] adaptorChannel]
closeChannel];
}
}
}
The next time a channel is needed, its EODatabaseContext reopens it automatically.
The closeChannels method above assumes that the application only has one EOObjectStoreCoordinator. If your application has multiple coordinators, you would repeat the process for each coordinator. It also assumes that all of the EOCooperatingObjectStores managed by the coordinator are EODatabaseContexts, which is nearly always the case. An EOObjectStoreCoordinator uses only EODatabaseContexts unless you substitute your own EOCooperatingObjectStore subclass. (For more information about EOCooperatingObjectStores, see the chapter "Application Configurations".)
Table of Contents
Next Section